package io.light.frame.dal.mybatis.generator.core.context;

import io.light.frame.dal.mybatis.generator.core.meta.entity.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.io.File;

/**
 * Generation result
 *
 * @author Ivan
 * @version 1.0.0
 * @date 2021-06-14 11:44
 */
@Getter
@Setter
@ToString
public class GenResult {

    private Detail detail;

    private boolean success;

    private String message;

    private ErrorStack errorStack;

    public boolean isSuccess() {
        return errorStack == null || errorStack.isEmpty();
    }

    public void addErr(Exception err) {
        if (errorStack == null) {
            errorStack = new ErrorStack();
        }
        errorStack.push(ErrorStack.Item.of(err));
    }

    public String getMessage() {
        if (StringUtils.isNotBlank(message)) {
            return message;
        }
        return isSuccess() ? "success" : errorStack.getMessage();
    }

    @Getter
    @Setter
    @ToString
    public static class Detail {
        private String moduleName;
        private File moduleDir;
        private File designXml;
        private Table table;
        private File entityFile;
        private File daoFile;
        private File mapperXml;

        public static Detail from(GenerationContext context) {
            Detail detail = new Detail();
            detail.setModuleName(context.getModule());
            detail.setModuleDir(context.getProjectDir());
            detail.setDesignXml(context.getDesignXml().getFile());
            detail.setTable(context.getMapper().getTable());
            return detail;
        }
    }
}
